package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;


@Service
@Transactional
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper,AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //判断参数是否为空，为空直接给前端返回参数无效
        if(dto==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查dto对象分页是否合理，不合理设置默认分页
        dto.checkParam();
        //创建分页对象
        Page<AdChannel> pageReq = new Page<>(dto.getPage(), dto.getSize());
        //设置查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper= Wrappers.<AdChannel>lambdaQuery();
        //如果名字不为空，设置模糊查询条件
        if (StringUtils.isNoneBlank(dto.getName())){
            queryWrapper.like(AdChannel::getName,dto.getName());
        }
        //获得查询结果对象
        IPage<AdChannel> pageResult = page(pageReq, queryWrapper);
        //获取分页结果对象
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) pageResult.getTotal());
        //设置时间
        pageResponseResult.setData(pageResult.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdChannel channel) {
        //判断参数是否无效
        if(channel==null || StringUtils.isBlank(channel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否是默认频道
        if (channel.getIsDefault()){
            //将原有的默认频道改为普通频道
            AdChannel one = getOne(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getIsDefault, 1));
            if (one!=null){
                one.setIsDefault(false);
                updateById(one);
            }
        }
        //设置插入频道时间
        channel.setCreatedTime(new Date());
        save(channel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        //判断参数是否无效
        if (adChannel==null || StringUtils.isBlank(adChannel.getName()) ||
                adChannel.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id获取当前修改频道对象
        AdChannel channel = getById(adChannel.getId());
        //如果为空
        if (channel==null){
            //返回无数据
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //不为空修改数据
        updateById(adChannel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //判断参数
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //根据id获取要删除的频道对象
        AdChannel channel = getById(id);
        //是否为空
        if (channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断是否在使用，在使用无法删除
        if (channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEl_USED);
        }
        //删除频道
        removeById(id);
        return ResponseResult.okResult();
    }
}
